const express = require('express')
const log = require('../log')
const router = express.Router();
const sql = require('../sql')
const md5 = require('js-md5')
router.get('/HelloWorld', function (req, res) {
    log('INFO', 'API', '/HelloWorld', 'GET', "request!");
    res.json({
        msg: "Hello World!"
    });
})
router.post('/login', function (req, res) {
    log('INFO', 'API', '/login', 'POST', 'request!')
    var data = req.body;
    sql.getSqlConnection().query('select * from user where username = ? and password = ?', [data.username, md5(data.password)], function (err, rows) {
        if (err) {
            log('ERROR', 'API', '/login', 'POST', err);
            res.json({
                code: 500,
                errorModule: "API-SQL",
                errorMsg: err
            })
            return;
        }
        if (rows.length > 0) {
            res.json({
                code: 200,
                data: {
                    uid: rows[0].uid,
                    username: rows[0].username,
                    role: rows[0].role
                }
            })
            return;
        } else {
            res.json({
                code: 200,
                data: "NO DATA"
            })
            return;
        }
    })
})
router.post('/myCardGroups', function (req, res) {
    var data = req.body;
    sql.getSqlConnection().query("select * from card_group where card_group_create_uid = ?", [data.uid], function (err, rows) {
        if (err) {
            log('ERROR', 'API', '/login', 'POST', err);
            res.json({
                code: 500,
                errorModule: "API-SQL",
                errorMsg: err
            })
            return;
        }
        res.json({
            code: 200,
            dataLength: rows.length,
            data: rows
        })
        return;
    })
})
router.post('/createMyGroups', function (req, res) {
    var data = req.body;
    sql.getSqlConnection().query('select * from card_group where card_group_name = ? and card_group_create_uid = ?', [data.groupName, data.createUid], function (err, rows) {
        if (err) {
            log('ERROR', 'API', '/login', 'POST', err);
            res.json({
                code: 500,
                errorModule: "API-SQL",
                errorMsg: err
            })
            return;
        }
        if (rows.length > 0) {
            res.json({
                code: 200,
                groupID: -1,
                msg: "group Name Exisit!"
            })
            return;
        } else {
            sql.getSqlConnection().query('insert into card_group (card_group_name,card_group_create_uid,card_group_public) value (?,?,?)', [data.groupName, data.createUid, 0], function (err, rows) {
                if (err) {
                    log('ERROR', 'API', '/login', 'POST', err);
                    res.json({
                        code: 500,
                        errorModule: "API-SQL",
                        errorMsg: err
                    })
                    return;
                }
                sql.getSqlConnection().query('select * from card_group where card_group_name = ? and card_group_create_uid = ?', [data.groupName, data.createUid], function (err, rows) {
                    if (err) {
                        log('ERROR', 'API', '/login', 'POST', err);
                        res.json({
                            code: 500,
                            errorModule: "API-SQL",
                            errorMsg: err
                        })
                        return;
                    }
                    if (rows.length > 0) {
                        res.json({
                            code: 200,
                            data: {
                                GroupID: rows[0].card_group_uid
                            }
                        })
                        return;
                    } else {
                        res.json({
                            code: 200,
                            error: "ERR"
                        })
                    }
                })
            })
        }
    })
})
router.post('/addCard',function(req,res){
    var data = req.body;
    log('INFO', 'API', '/addCard', 'POST', 'request!')
    sql.getSqlConnection().query('insert into card (card_name,card_noted,card_example,card_create_uid,card_group_id) value (?,?,?,?,?)',[data.cardTitle,data.cardNoted,"",data.createUid,data.groupUid],function(err,rows){
        if (err) {
            log('ERROR', 'API', '/login', 'POST', err);
            res.json({
                code: 500,
                errorModule: "API-SQL",
                errorMsg: err
            })
            return;
        }
    })
})
router.post('/getCardCount',function(req,res){
    var data = req.body;
    sql.getSqlConnection().query("select * from card where card_group_id = ?",[data.groupID],function(err,rows){
        if (err) {
            log('ERROR', 'API', '/login', 'POST', err);
            res.json({
                code: 500,
                errorModule: "API-SQL",
                errorMsg: err
            })
            return;
        }
        res.json({
            code:200,
            cardCount:rows.length
        })
        return;
    })
})
router.post('/getCards',function(req,res){
    var data = req.body;
    sql.getSqlConnection().query("select * from card where card_group_id = ?",[data.groupID],function(err,rows){
        if (err) {
            log('ERROR', 'API', '/login', 'POST', err);
            res.json({
                code: 500,
                errorModule: "API-SQL",
                errorMsg: err
            })
            return;
        }
        res.json({
            code:200,
            dataLength:rows.length,
            data:rows
        })
        return;
    })
})
module.exports = router;